package com.masterclient.app.modules.shop;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseActivity;
import com.masterclient.app.R;
import com.masterclient.app.modules.shop.beans.SellerBean;
import com.masterclient.app.modules.shop.interfacess.SellerBottomCallback;
import com.masterclient.app.modules.shop.interfacess.SellerInfoCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/4/15.
 */
@Route(path = AroutePath.STORE_ACTIVITY)
public class StoreActivity extends BaseActivity implements SellerInfoCallback {


    @BindView(R.id.store_bottom)
    LinearLayout storeBottom;
    @BindView(R.id.store_index_icon)
    ImageView storeIndexIcon;
    @BindView(R.id.store_index_name)
    TextView storeIndexName;
    @BindView(R.id.store_index_ll)
    LinearLayout storeIndexLl;
    @BindView(R.id.store_classify_icon)
    ImageView storeClassifyIcon;
    @BindView(R.id.store_classify_name)
    TextView storeClassifyName;
    @BindView(R.id.store_classify_ll)
    LinearLayout storeClassifyLl;
    @BindView(R.id.store_content)
    FrameLayout storeContent;

    @Autowired(name = "store_id")
    String store_id;
    @Autowired(name = "is_dealer")
    String isDealer;
    @Autowired(name = "is_agent")
    String isAgent;

    private FragmentManager fragmentManager;
    private StoreFragment storeFragment;
    private StoreClassifyFragment classifyFragment;
    public String isFavorite;
    public String onSaleTitle;
    public SellerBean sellerBean;
    private Fragment currentFragment;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);

        isDealer = TextUtils.isEmpty(isDealer)?"0":isDealer;
        isAgent = TextUtils.isEmpty(isAgent)?"0":isAgent;

        storeFragment = StoreFragment.getInstance(store_id,isDealer,isAgent);
        classifyFragment = StoreClassifyFragment.getInstance(store_id);
        storeFragment.setSellerInfoCallback(this);
        classifyFragment.setSellerInfoCallback(this);
        storeFragment.setSellerBottomCallback(new SellerBottomCallback() {
            @Override
            public void onState(boolean isHidden) {
                storeBottom.setVisibility(isHidden?View.GONE:View.VISIBLE);
            }
        });


        fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().add(R.id.store_content, storeFragment).commit();
        currentFragment = storeFragment;
    }


    @OnClick({R.id.store_index_ll, R.id.store_classify_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.store_index_ll:
                storeIndexIcon.setImageResource(R.mipmap.store_index_select);
                storeIndexName.setTextColor(0xfff23051);
                storeClassifyIcon.setImageResource(R.mipmap.store_classify_unselect);
                storeClassifyName.setTextColor(0xff999faa);
                switchContent(currentFragment, storeFragment);
                break;
            case R.id.store_classify_ll:
                storeIndexIcon.setImageResource(R.mipmap.store_index_unselect);
                storeIndexName.setTextColor(0xff999faa);
                storeClassifyIcon.setImageResource(R.mipmap.store_classify_select);
                storeClassifyName.setTextColor(0xfff23051);
                switchContent(currentFragment, classifyFragment);
                break;
        }
    }

    public void switchContent(Fragment from, Fragment to) {
        if (currentFragment != to) {
            currentFragment = to;
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            if (!to.isAdded()) {
                transaction.hide(from).add(R.id.store_content, to).commitAllowingStateLoss();
            } else {
                transaction.hide(from).show(to).commitAllowingStateLoss();
            }
        }
    }


    @Override
    public void onSellerInfo(String favoriteState, String onSaleTitle, SellerBean sellerBean) {
        isFavorite = favoriteState;
        if (sellerBean != null) {
            this.sellerBean = sellerBean;
            this.onSaleTitle = onSaleTitle;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        scheduleTimer();
    }


}
